<?php

namespace app\api\controller\project;

use think\Controller;

class Project extends Controller{
    public function __construct()
    {
        parent::__construct();

        $this->ProjectModel=model('Project.Project');
        $this->OrderModel=model('Project.Order');
        $this->UserModel=model('User.User');
        $this->RecordModel=model('User.Record');
        $this->AddressModel=model('User.Address');
    }

    public function index(){
        if($this->request->isPost()){
            $userid=$this->request->param('userid',0);
            $status=$this->request->param('status','');

            $user=$this->UserModel->find($userid);

            if(!$user){
                $this->error('用户不存在');
                exit;
            }

            $where=[
                'userid'=>$userid
            ];
            if($status != ""){
                $where['status']=$status;
            }

            $order=$this->OrderModel->with(['project'])->where($where)->select();

            $this->success('返回预约列表',null,$order);
            exit;
        }
    }

    public function add(){
        if($this->request->isPost()){
            $params=$this->request->param();

            $userid=$this->request->param('userid',0);
            $addrid=$this->request->param('addrid',0);
            $projectid=$this->request->param('projectid',0);
            $createtime=$this->request->param('createtime',0);

            $user=$this->UserModel->find($userid);

            if(!$user){
                $this->error('用户不存在');
                exit;
            }

            $address=$this->AddressModel->find($addrid);

            if(!$address){
                $this->error('收货地址不存在');
                exit;
            }

            //判断这个收货地址是不是这个用户的
            if($address['userid'] != $userid)
            {
                $this->error('非法选择上门地址');
                exit;
            }

            $project=$this->ProjectModel->find($projectid);
            if(!$project){
                $this->error('预约的服务不存在，请重新填写');
                exit;
            }

            $now=time()+3600*4;
            if($now > $createtime){
                $this->error('请选择有效时间，预约在当前时间的4个小时后');
                exit;
            }

            $data=[
                'userid'=>$userid,
                'addrid'=>$addrid,
                'projectid'=>$projectid,
                'content'=>$params['content'],
                'createtime'=>$createtime,
                'status'=>0
            ];

            $result=$this->OrderModel->validate('common/Project/Order')->save($data);

            if($result===FALSE){
                $this->error($this->OrderModel->getError());
                exit;
            }else{
                $this->success('预约成功，请等待客服联系','/pages/project/index');
                exit;
            }
        }
    }

    public function info(){
        if($this->request->isPost()){
            $proid=$this->request->param('proid',0);
            $userid=$this->request->param('userid',0);

            $where=[
                'order.id'=>$proid,
                'order.userid'=>$userid
            ];

            $order=$this->OrderModel->alias('order')->with(['project'])->where($where)->find();

            if(!$order){
                $this->error('暂无预约记录');
                exit;
            }else{
                $this->success('返回预约记录',null,$order);
                exit;
            }
        }
    }

    public function pay(){
        if($this->request->isPost()){
            $userid=$this->request->param('userid',0);
            $proid=$this->request->param('proid',0);

            $where=[
                'order.userid'=>$userid,
                'order.id'=>$proid
            ];

            $order=$this->OrderModel->alias('order')->with(['project'])->where($where)->find();

            if(!$order){
                $this->error('预约记录不存在');
                exit;
            }

            $user=$this->UserModel->find($userid);

            if(!$user){
                $this->error('用户不存在');
                exit;
            }

            $UserPoint=$user['point'];


            $OrderPoint=$order['point'];

            $UpdatePoint=bcsub($UserPoint,$OrderPoint);

            if($UserPoint < 0){
                $this->error('用户积分不足');
                exit;
            }

            // 开启事务
            $this->OrderModel->startTrans();
            $this->UserModel->startTrans();
            $this->RecordModel->startTrans();

            $OrderData=[
                'id'=>$proid,
                'status'=>2
            ];

            $OrderStatus=$this->OrderModel->isUpdate(true)->save($OrderData);

            if($OrderStatus===FALSE){
                $this->error($this->OrderModel->getError());
                exit;
            }

            $UserData=[
                'id'=>$userid,
                'point'=>$UpdatePoint
            ];
            $UserStatus=$this->UserModel->isUpdate(true)->save($UserData);

            if($UserStatus===FALSE){
                $this->OrderModel->rollback();
                $this->error($this->UserModel->getError());
                exit;
            }

            $RecordData=[
                'point'=>$OrderPoint,
                'content'=>"预约服务：".$order['project']['name']."---消费的积分为：$OrderPoint",
                'state'=>6,
                'userid'=>$userid
            ];

            $RecordStatus=$this->RecordModel->validate('common/User/Record')->save($RecordData);

            if($RecordStatus===FALSE){
                $this->UserModel->rollback();
                $this->OrderModel->rollback();

                $this->error($this->RecordModel->getError());
                exit;
            }

            if($OrderStatus === FALSE || $UserStatus === FALSE || $RecordStatus === FALSE){
                $this->RecordModel->rollback();
                $this->UserModel->rollback();
                $this->OrderModel->rollback();

                $this->error('支付失败');
                exit;
            }else{
                $this->RecordModel->commit();
                $this->UserModel->commit();
                $this->OrderModel->commit();
                $this->success('支付成功，等待平台处理，安排上门服务');
                exit;
            }
        }
    }

    public function revoke(){
        if($this->request->isPost()){
            $userid=$this->request->param('userid',0);
            $proid=$this->request->param('proid',0);

            $where=[
                'order.userid'=>$userid,
                'order.id'=>$proid
            ];

            $order=$this->OrderModel->alias('order')->with(['project'])->where($where)->find();

            if(!$order){
                $this->error('用户不存在');
                exit;
            }

            $OrderData=[
                'id'=>$proid,
                'status'=>-1
            ];

            $result=$this->OrderModel->isUpdate(true)->save($OrderData);

            if($result===FALSE){
                $this->error($this->OrderModel->getError());
                exit;
            }else{
                $this->success('撤销申请成功。请等待平台审核');
                exit;
            }
        }
    }

    public function rate(){
        if($this->request->isPost()){
            $userid=$this->request->param('userid',0);
            $proid=$this->request->param('proid',0);
            $rate=$this->request->param('rate',0);

            $where=[
                'order.userid'=>$userid,
                'order.id'=>$proid
            ];

            $order=$this->OrderModel->alias('order')->with(['project'])->where($where)->find();

            if(!$order){
                $this->error('预约记录不存在');
                exit;
            }

            $user=$this->UserModel->find($userid);

            if(!$user){
                $this->error('用户不存在');
                exit;
            }

            $OrderData=[
                'id'=>$proid,
                'rate'=>$rate
            ];

            $result=$this->OrderModel->isUpdate(true)->save($OrderData);

            if($result===FALSE){
                $this->error($this->OrderModel->getError());
                exit;
            }else{
                $this->success('评分成功');
                exit;
            }
        }
    }
}